import mongoose from 'mongoose';
import logger from './logger';
import config from './config';

mongoose.Promise = global.Promise;

const connectToDB = async () => {
  let dbHost = config.dbHost;
  let dbPort = config.dbPort;
  let dbName = config.dbName;

  try {
    await mongoose.connect(`mongodb://${dbHost}:${dbPort}/${dbName}`);
    logger.info('Connected to mongo!!!');
  } catch (error) {
    logger.error('Could not connect to MongoDB' + JSON.stringify(error));
  }
};

export default connectToDB;
